/*
 * Copyright (c) 2002-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant.ant.taskdefs;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.PatternSet;

import org.emergent.antbite.savant.Artifact;
import org.emergent.antbite.savant.ArtifactGroup;
import org.emergent.antbite.savant.LocalCacheStore;
import org.emergent.antbite.savant.SavantException;
import org.emergent.antbite.savant.ant.types.ArtifactGroupType;
import org.emergent.antbite.savant.ant.types.ArtifactType;


/**
 * <p>
 * This class is used to copy artifacts from the local cache
 * store to a directory. This class can be used the same as
 * a normal copy task, but also supports nested artifact
 * and artifactgroup elements.
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class ArtifactCopyTask extends Copy {

    private File localCache;
    private final List artifacts = new ArrayList();
    private final List groups = new ArrayList();


    /**
     * Sets the location of the local cache. This can also be changed via a
     * System property. See {@link LocalCacheStore} for more details.
     *
     * @param   localCache The local cache location
     */
    public void setLocalcache(File localCache) {
        this.localCache = localCache;
    }

    /**
     * Adds a new artifact definition to this list of artifacts to copy.
     *
     * @param   artifact The artifact to copy
     */
    public void addArtifact(ArtifactType artifact) {
        artifacts.add(artifact.getProxy());
    }

    /**
     * Adds a new artifact group definition to this list of artifacts to copy.
     *
     * @param   group The new group
     */
    public void addArtifactgroup(ArtifactGroupType group) {
        groups.add(group.getProxy());
    }

    /**
     * Performs the copy of all the artifacts.
     */
    public void execute() {
        LocalCacheStore lcs;
        try {
            lcs = new LocalCacheStore(localCache);
        } catch (SavantException se) {
            log("Invalid local cache location [" + localCache.getAbsolutePath() + "]");
            throw new BuildException(se);
        }

        FileSet fs = new FileSet();
        fs.setProject(getProject());
        fs.setDir(lcs.getLocation());
        addIncludes(fs, artifacts);

        for (int i = 0; i < groups.size(); i++) {
            ArtifactGroup artifactGroup = (ArtifactGroup) groups.get(i);
            addIncludes(fs, artifactGroup.getArtifacts());
        }

        super.addFileset(fs);
        super.execute();
    }

    /**
     * Creates a file set include definition for all the artifacts in the given
     * list.
     */
    private void addIncludes(FileSet fs, List artifacts) {
        for (int i = 0; i < artifacts.size(); i++) {
            Artifact artifact = (Artifact) artifacts.get(i);
            PatternSet.NameEntry ne = fs.createInclude();
            ne.setName(artifact.getArtifactFile());
        }
    }
}